
package bd_jugueteria;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

public class cliente_empleado extends javax.swing.JFrame {

    Connection conector;
    ventana principal;
    
    public cliente_empleado(ventana v,Connection c) throws SQLException {
        initComponents();
        conector =c;
        principal = v;   
        
        jLabel4.setVisible(false);
        jTextField5.setVisible(false);
        
        
        jLabel5.setVisible(false);
        jTextField6.setVisible(false);
        
        
        Statement st = conector.createStatement();

                ResultSet r = st.executeQuery("Select idcliente from cliente order by idcliente desc limit 1");
                r.next();
                System.out.println(r.getString("idcliente"));
                MostrarCliente(r.getString("idcliente"));
        
        
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jComboBox1 = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jTextField5 = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTextField6 = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jTable1);

        getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 72, 771, 219));

        jComboBox1.setFont(new java.awt.Font("Papyrus", 3, 11)); // NOI18N
        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "cliente", "empleado" }));
        jComboBox1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox1ItemStateChanged(evt);
            }
        });
        getContentPane().add(jComboBox1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 44, -1, -1));

        jButton1.setFont(new java.awt.Font("Papyrus", 3, 11)); // NOI18N
        jButton1.setText("Buscar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 40, -1, -1));
        getContentPane().add(jTextField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 40, 134, -1));
        getContentPane().add(jTextField2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 369, 175, -1));

        jLabel1.setFont(new java.awt.Font("Comic Sans MS", 3, 12)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Nombre completo");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 340, -1, -1));

        jLabel2.setFont(new java.awt.Font("Comic Sans MS", 3, 12)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setText("Direccion");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 340, -1, -1));
        getContentPane().add(jTextField3, new org.netbeans.lib.awtextra.AbsoluteConstraints(195, 369, 102, -1));

        jLabel3.setFont(new java.awt.Font("Comic Sans MS", 3, 12)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(255, 255, 255));
        jLabel3.setText("Telefono");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 340, -1, -1));
        getContentPane().add(jTextField4, new org.netbeans.lib.awtextra.AbsoluteConstraints(315, 369, 108, -1));

        jButton2.setFont(new java.awt.Font("Papyrus", 3, 11)); // NOI18N
        jButton2.setText("Eliminar");
        getContentPane().add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 302, -1, -1));

        jButton3.setFont(new java.awt.Font("Papyrus", 3, 11)); // NOI18N
        jButton3.setText("Actualizar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(108, 302, -1, -1));

        jButton4.setFont(new java.awt.Font("Papyrus", 3, 11)); // NOI18N
        jButton4.setText("VOLVER");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 407, -1, 33));

        jButton5.setFont(new java.awt.Font("Papyrus", 3, 11)); // NOI18N
        jButton5.setText("Agregar");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton5, new org.netbeans.lib.awtextra.AbsoluteConstraints(156, 412, 87, -1));

        jLabel4.setFont(new java.awt.Font("Comic Sans MS", 3, 12)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(255, 255, 255));
        jLabel4.setText("Sueldo");
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(470, 340, -1, -1));
        getContentPane().add(jTextField5, new org.netbeans.lib.awtextra.AbsoluteConstraints(449, 369, 98, -1));

        jLabel5.setFont(new java.awt.Font("Comic Sans MS", 3, 12)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(255, 255, 255));
        jLabel5.setText("Password");
        getContentPane().add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(600, 340, 70, -1));
        getContentPane().add(jTextField6, new org.netbeans.lib.awtextra.AbsoluteConstraints(565, 369, 114, -1));

        jLabel6.setBackground(new java.awt.Color(153, 153, 255));
        jLabel6.setToolTipText("");
        jLabel6.setOpaque(true);
        getContentPane().add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 790, 450));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // TODO add your handling code here:

        if(jTextField2.getText().length()> 0 && jTextField3.getText().length()> 0 && jTextField4.getText().length() > 0){
        
            
        if(jButton3.getText().equals("Guardar Cambios")) {
        } else {
            try {
                
                   if(jComboBox1.getSelectedIndex() == 0 ){//CLIENTE
                
                try (PreparedStatement insertar = conector.prepareStatement("insert into "
                        + "cliente(nombre,direccion,telefono) "
                        + " values(?,?,?)")) {
                    insertar.setString(1,jTextField2.getText());
                    insertar.setString(2,jTextField3.getText());
                    insertar.setString(3,jTextField4.getText());
                    
                    insertar.execute();
                    insertar.close();
                    JOptionPane.showMessageDialog(null, "Cliente Agregado");
                    
                    Statement st = conector.createStatement();
                    
                    ResultSet r = st.executeQuery("Select idcliente from cliente order by idcliente desc limit 1");
                    r.next();
                    
                    MostrarCliente(r.getString("idcliente"));
                    
                    jTextField2.setText("");
                    jTextField3.setText("");
                    jTextField4.setText("");
                
                    
                }//TRY
                    
                }//CLIENTE
                else{  
                       if(jTextField5.getText().length() > 0 && jTextField6.getText().length() > 0){
                        
                           
                              try (PreparedStatement insertar = conector.prepareStatement("insert into "
                        + "empleado(nombre,telefono,sueldo,cargo,pass) "
                        + " values(?,?,?,?,?)")) {
                    insertar.setString(1,jTextField2.getText());//nombre
                    insertar.setString(2,jTextField4.getText());//telefono
                    insertar.setString(3,jTextField5.getText());//sueldo
                    insertar.setString(4,jTextField3.getText());//cargo
                    insertar.setString(5,jTextField6.getText());//pass

                    insertar.execute();
                       }
                    JOptionPane.showMessageDialog(null, "Empleado Agregado");
                    
                    Statement st = conector.createStatement();
                    
                    ResultSet r = st.executeQuery("Select idempleado from empleado order by idempleado desc limit 1");
                    r.next();
                    
                    MostrarCliente(r.getString("idempleado"));
                    
                    jTextField2.setText("");
                    jTextField3.setText("");
                    jTextField4.setText("");     
                    jTextField5.setText("");     
                    jTextField6.setText("");
                       
                        }//if tenemos algo que agregar
                    
                           
                       }//else empleado
                    
                    
            } catch (SQLException e) {
                System.out.print(e.getMessage());
                //JOptionPane.showMessageDialog(null, "ID (" + camp1.getText() + ") en uso..");
            }//CATCH
        }//ELSE
            
        }
        
    }//GEN-LAST:event_jButton5ActionPerformed

    public void actualizaCliente(int fila2){
        if(jButton3.getText().equals("Actualizar")){
        if (fila2 >= 0) {
            //jug1.setText(Tjuguete.getValueAt(fila2, 0).toString());
            jTextField2.setText(jTable1.getValueAt(fila2, 1).toString());
            jTextField3.setText(jTable1.getValueAt(fila2, 2).toString());
            jTextField4.setText(jTable1.getValueAt(fila2, 3).toString());
            jButton3.setText("Guardar Cambios");
            
        } else {
            JOptionPane.showMessageDialog(null, "Seleccionado una fila por favor");
        }
        
            
        }
        else{
            
          jButton3.setText("Actualizar");
            
            try {

            PreparedStatement pre = conector.prepareStatement("UPDATE cliente "
                + "SET nombre = '" + jTextField2.getText()+ "', direccion='" + jTextField3.getText() + "', telefono='" + jTextField4.getText()+"' WHERE idcliente='" +
                    jTable1.getValueAt(fila2, 0).toString() + "';");
            pre.executeUpdate();

            
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            
         
            JOptionPane.showMessageDialog(this, "Actualizado con exito");
            MostrarCliente("");
        } catch (SQLException ex) {
            //JOptionPane.showMessageDialog(this, "Error al Corregir");
            System.out.print(ex.getMessage());
         
         }
         }
        
    }
    
    
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        
        int fila2 = jTable1.getSelectedRow();
        
        if(jComboBox1.getSelectedIndex() == 0 ){
            actualizaCliente(fila2);
        }
        else
            actualizaEmpleado(fila2);
        
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        
        if(jComboBox1.getSelectedIndex() == 0 )
         MostrarCliente(jTextField1.getText());
        else
            MostrarEmpleado(jTextField1.getText());
        
        
        jTextField1.setText("");
        
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        
        this.setVisible(false);
        principal.setVisible(true);
        
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox1ItemStateChanged
        // TODO add your handling code here:
        
        if(jComboBox1.getSelectedIndex()==0){
            
            
            jLabel2.setText("Direccion");
            jLabel3.setText("Telefono");
            jLabel4.setVisible(false);
            jTextField5.setVisible(false);
            jLabel5.setVisible(false);
            jTextField6.setVisible(false);
            
            MostrarCliente("");
            
        }else
        {
            
            jLabel2.setText("Telefono");
            jLabel3.setText("Sueldo");
            jLabel4.setText("Cargo");
            jLabel4.setVisible(true);
            jTextField5.setVisible(true);
            jLabel5.setVisible(true);
            jTextField6.setVisible(true);
            
      
            MostrarEmpleado("");
        }
        
        
    }//GEN-LAST:event_jComboBox1ItemStateChanged

    public static void main(String args[]) {
      
       
    }
    
    
    void MostrarCliente(String muestra) {
        DefaultTableModel ModeloCliente = new DefaultTableModel();

        ModeloCliente.addColumn("ID Cliente");
        ModeloCliente.addColumn("Nombre");
        ModeloCliente.addColumn("Direccion");
        ModeloCliente.addColumn("Telefono");

        jTable1.setModel(ModeloCliente);
        String sql = "";
        if (muestra.equals("")) {

            sql = "SELECT * FROM cliente";
        } else {
            sql = "SELECT * FROM cliente WHERE idcliente ='" + muestra + "'";
        }
        String[] datosempleado = new String[4];
        try {
            Statement st = conector.createStatement();
            ResultSet rs = st.executeQuery(sql);
            while (rs.next()) {
                datosempleado[0] = rs.getString(1);
                datosempleado[1] = rs.getString(2);
                datosempleado[2] = rs.getString(3);
                datosempleado[3] = rs.getString(4);

                ModeloCliente.addRow(datosempleado);

            }
            jTable1.setModel(ModeloCliente);
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
    }

    void MostrarEmpleado(String empleado) {
        DefaultTableModel ModeloEmpleado = new DefaultTableModel();

        ModeloEmpleado.addColumn("ID Empleado");
        ModeloEmpleado.addColumn("Nombre");
         ModeloEmpleado.addColumn("Telefono");
        ModeloEmpleado.addColumn("Sueldo");
        ModeloEmpleado.addColumn("Cargo");
        ModeloEmpleado.addColumn("pass");
        
        jTable1.setModel(ModeloEmpleado);
        String sql = "";
        if (empleado.equals("")) {

            sql = "SELECT * FROM empleado";
        } else {
            sql = "SELECT * FROM empleado WHERE idempleado ='" + empleado + "'";
        }
        String[] datosEmpleado = new String[6];
        try {
            Statement stat = conector.createStatement();
            ResultSet rs = stat.executeQuery(sql);
            while (rs.next()) {
                datosEmpleado[0] = rs.getString(1);
                datosEmpleado[1] = rs.getString(2);
                datosEmpleado[2] = rs.getString(3);
                datosEmpleado[3] = rs.getString(4);
                datosEmpleado[4] = rs.getString(5);
                datosEmpleado[5] = rs.getString(6);

                ModeloEmpleado.addRow(datosEmpleado);

            }
            jTable1.setModel(ModeloEmpleado);
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
    }
    
    
    
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    // End of variables declaration//GEN-END:variables

    private void actualizaEmpleado(int fila2) {
        
        if(jButton3.getText().equals("Actualizar")){
        if (fila2 >= 0) {
            //jug1.setText(Tjuguete.getValueAt(fila2, 0).toString());
            jTextField2.setText(jTable1.getValueAt(fila2, 1).toString());
            jTextField3.setText(jTable1.getValueAt(fila2, 2).toString());
            jTextField4.setText(jTable1.getValueAt(fila2, 3).toString());
            jTextField5.setText(jTable1.getValueAt(fila2, 4).toString());
            jTextField6.setText(jTable1.getValueAt(fila2, 5).toString());
            jButton3.setText("Guardar Cambios");
            
        } else {
            JOptionPane.showMessageDialog(null, "Seleccionado una fila por favor");
        }
        
            
        }
        else{
            
          jButton3.setText("Actualizar");
            
            try {

            PreparedStatement pre = conector.prepareStatement("UPDATE empleado "
                + "SET nombre = '" + jTextField2.getText()+ "', telefono='" + jTextField3.getText() + "', sueldo='" + jTextField4.getText()
                    + "', cargo='" + jTextField5.getText()+ "', pass='" + jTextField6.getText()+"' WHERE idempleado='" +
                    jTable1.getValueAt(fila2, 0).toString() + "';");
            pre.executeUpdate();

            
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            
         
            JOptionPane.showMessageDialog(this, "Actualizado con exito");
            MostrarEmpleado("");
        } catch (SQLException ex) {
            //JOptionPane.showMessageDialog(this, "Error al Corregir");
            System.out.print(ex.getMessage());
         
         }
         }
        
        
        
    }
}
